<?php

namespace App\Http\Controllers\storage;

use App\Http\Controllers\Controller;

class MovementController extends Controller
{
    public function index()
    {
        $menu_id = request('curr_menu_id', 0);
        if(!$menu_id){
            return response()->json('非法操作');
        }
        $toolBarButton = $this->buildToolBarButton($menu_id);
        return view('storage.movement.list',compact('menu_id','toolBarButton'));
    }

    public function listData()
    {
        $params['curr_menu_id'] = request('curr_menu_id', 0);
        if(!$params['curr_menu_id']){
            return response()->json(['code'=>400, '页面无菜单ID']);
        }
        $params['page'] = request('page', 1);
        $params['rows'] = request('rows', 10);
        try{
            $list = callApiServer("Storage.MovementService.searchList", $params);
            return response()->json($list);
        }catch (\Exception $e){
            if($e->getCode() == 500){
                return response()->json(['code'=>400,'data'=>'后台服务异常:'.$e->getMessage()]);
            }else{
                return response()->json(['code'=>400,'data'=>$e->getMessage()]);
            }
        }
    }

    public function listDataGoods(){
        $params['id'] = request('movement_id');
        try{
            $list = callApiServer("Storage.MovementService.goodsList", $params);
            return response()->json($list);
        }catch (\Exception $e){
            if($e->getCode() == 500){
                return response()->json(['code'=>400,'data'=>'后台服务异常:'.$e->getMessage()]);
            }else{
                return response()->json(['code'=>400,'data'=>$e->getMessage()]);
            }
        }
    }

    public function showAddForm()
    {
        return view('storage.movement.form');
    }

    public function showEditForm()
    {
        $params['id'] = request('dict_id');
        try{
            $info = callApiServer("system.DictService.getInfoById", $params);
            if(empty($info)){
                return response()->json('对应数据不存在');
            }
            return view('storage.movement.form', compact("info"));
        }catch (\Exception $e){
            if($e->getCode() == 500){
                return response()->json(['code'=>500,'data'=>'后台服务异常:'.$e->getMessage()]);
            }else{
                return response()->json(['code'=>400,'data'=>$e->getMessage()]);
            }
        }
    }

    public function movementAdd()
    {
        $params['id'] = request('id',0);
        $params['doctype_id'] = request('doctype_id');
        $params['movementdate'] = request('movementdate');
        $params['warehouse_id'] = request('warehouse_id');
        $params['warehouseto_id'] = request('warehouseto_id');
        $params['description'] = request('description',0);
        if(empty($params['warehouse_id'])){
            return response()->json(['code'=>400,'data'=>'调出仓库不能为空']);
        }
        if(empty($params['warehouseto_id'])){
            return response()->json(['code'=>400,'data'=>'调入仓库不能为空']);
        }
        if(empty($params['doctype_id'])){
            return response()->json(['code'=>400,'data'=>'调拨类型不能为空']);
        }
        if(empty($params['description'])){
            return response()->json(['code'=>400,'data'=>'调拨描述不能为空']);
        }
        $rs = callApiServer("Storage.MovementService.saveData", $params);
        if($rs){
            return response()->json(['code'=>200,'id'=>$rs]);
        }
        return response()->json(['code'=>400,'data'=>$rs]);
    }
}
